/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.rebate;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.entity.DisDealerAcct;
import com.sigmatrix.h5.enums.CrmAcctType;
import com.sigmatrix.h5.enums.RebateTarget;
import com.sigmatrix.h5.enums.WalletType;
import com.sigmatrix.h5.service.DisRebateDetailService;
import com.sigmatrix.h5.service.ShortMessageService;
import com.sigmatrix.h5.service.common.ServiceHelper;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.SessionUtils;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.Result;
import com.sigmatrix.h5.web.args.PageParam;
import com.sigmatrix.h5.web.args.WithDrawCashParam;
import com.sigmatrix.h5.web.mode.RebateList;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import so.sao.sms.enums.AccountType;

/**
 *@ClassName: DisRebateDetail
 *@Description:
 *@author MH
 *@date 2017年8月11日 下午4:38:49
 */
@Api(tags="返利明细")
@RestController
@RequestMapping("/rebate/detail")
public class DisRebateDetailController extends BaseController{
	
	
	@Autowired
	private DisRebateDetailService disRebateDetailService;
	
//	@Autowired
//	private ShortMessageService shortMessageService;
//	/**
//	 * 获取返利明细
//	 *@Title: list
//	 *@Description: 
//	 *@return Result
//	 *@author MH
//	 *@date 2017年8月28日 上午11:06:15
//	 *@param dealerId
//	 *@param vo
//	 *@return
//	 */
//	@ApiOperation("获取返利明细")
//	@PostMapping("/api/list")
//	public Result<Object> list(@RequestBody PageParam vo){
////		if(CommonUtils.isEmpty(dealerId)){
////			return error("经销商ID不能为空");
////		}
//		Integer dealerId = null;
//		if(CrmAcctType.DEALER.value.equals(SessionUtils.getAcctType())){
//			dealerId = SessionUtils.getDealerId();
//		}else if(CrmAcctType.TERMINAL_SHOP.value.equals(SessionUtils.getAcctType())){
//			dealerId = SessionUtils.getTerminalShopId();
//		}else{
//			return error("只有经销商账户跟终端店账户才能查看");
//		}
//		return disRebateDetailService.selectRebateDetailsByDealerId(dealerId, vo.getPageNo(), vo.getPageSize());
//	}
	
//	@ApiOperation("审批通过以及提现明细")
//	@PostMapping("/api/list/v2")
//	public Result<RebateList> findRebateList(@RequestBody PageParam vo){
//		Integer dealerId = null;
//		if(RebateTarget.DEALER.value.equals(SessionUtils.getDealerType())){
//			dealerId = SessionUtils.getDealerId();
//		}else if(RebateTarget.TERMINAL_SHOP.value.equals(SessionUtils.getDealerType())){
//			dealerId = SessionUtils.getTerminalShopId();
//		}else if(RebateTarget.SALES_MAN.value.equals(SessionUtils.getDealerType())){
//			dealerId = SessionUtils.getUserId();
//		}else{
//			return error("只有经销商账户跟终端店账户才能查看");
//		}
//		
//		WalletType walletType = selectWalletType(SessionUtils.getEseCode(),SessionUtils.getAcctType(),SessionUtils.getUserType());
//		
//		if(walletType!=null&&CrmAcctType.isDealerAcct(SessionUtils.getAcctType())){
//			if(WalletType.PERSONAL_WALLET==walletType){					
//				dealerId = SessionUtils.getUserId();
//			}else{					
//				dealerId = SessionUtils.getDealerId();
//			}
//		}
//		
//		RebateList draw = disRebateDetailService.rebateList(dealerId, SessionUtils.getDealerType(), vo.getPageNo(), vo.getPageSize());
//		return ok(draw);
//	}
	
	
//	/**
//	 * 临时做法。
//	 * 选择零钱包
//	 * @param eseCode
//	 * @param acctType
//	 * @param useType
//	 * @return
//	 */
//	private WalletType selectWalletType(String eseCode,Short acctType,Short useType){
//		if("JGJ".equalsIgnoreCase(eseCode)||"JLJ001".equalsIgnoreCase(eseCode)||"jiuguijiu".equalsIgnoreCase(eseCode)){
//			if(CrmAcctType.isDealerAcct(acctType)){
//				return WalletType.ENT_WALLET;
//			}
//			if(CrmAcctType.isShopAcct(acctType)){
//				return WalletType.PERSONAL_WALLET;				
//			}
//			if(CrmAcctType.isEseAcct(acctType)){
//				return WalletType.PERSONAL_WALLET;
//			}
//		}else if("ZLSP".equalsIgnoreCase(eseCode)||"ZL001".equalsIgnoreCase(eseCode)){
//			if(CrmAcctType.isDealerAcct(acctType)){
//				return WalletType.PERSONAL_WALLET;
//			}
//			if(CrmAcctType.isShopAcct(acctType)){
//				return WalletType.PERSONAL_WALLET;				
//			}
//			if(CrmAcctType.isEseAcct(acctType)){
//				return WalletType.PERSONAL_WALLET;
//			}
//		}
//		return null;
//	}
	
	/**
	 * 提现
	 * 只试用于中粮。
	 *@Title: withDrawCash
	 *@Description: 
	 *@return Result
	 *@author MH
	 *@date 2017年8月28日 上午11:06:26
	 *@param vo
	 *@return
	 */
	@ApiOperation("余额宝提现")
	@PostMapping("/api/handleCash")
	public Result<Object> withDrawCash(@RequestBody WithDrawCashParam vo){
		final DisDealerAcct acct = new DisDealerAcct();
		if(CommonUtils.isNotEmpty(vo)){
			BeanUtils.copyProperties(vo, acct);
		}
		ServiceHelper.selectSmsService(null).validateCode(vo.getSmsCode());
		Integer userId = SessionUtils.getUserId();
		acct.setId(userId);
		if(CommonUtils.isEmpty(vo.getOpenId())|| "null".equals(vo.getOpenId()))return error("openId为空");
		
		return disRebateDetailService.sendWeChatRedPackage(acct, vo.getAmount());
	}
	
}
